load("@bazeldnf//bazeldnf:defs.bzl", "xattrs")
load("@io_bazel_rules_docker//contrib:group.bzl", "group_entry", "group_file")
load("@io_bazel_rules_docker//contrib:passwd.bzl", "passwd_entry", "passwd_file")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_oci//oci:defs.bzl", "oci_image")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//staging/src/kubevirt.io/client-go/version:def.bzl", "version_x_defs")

go_library(
    name = "go_default_library",
    srcs = ["virt-launcher.go"],
    importpath = "kubevirt.io/kubevirt/cmd/virt-launcher",
    visibility = ["//visibility:private"],
    deps = [
        "//pkg/apimachinery/wait:go_default_library",
        "//pkg/cloud-init:go_default_library",
        "//pkg/config:go_default_library",
        "//pkg/container-disk:go_default_library",
        "//pkg/downwardmetrics:go_default_library",
        "//pkg/ephemeral-disk:go_default_library",
        "//pkg/hooks:go_default_library",
        "//pkg/hotplug-disk:go_default_library",
        "//pkg/ignition:go_default_library",
        "//pkg/util:go_default_library",
        "//pkg/virt-config:go_default_library",
        "//pkg/virt-handler/cmd-client:go_default_library",
        "//pkg/virt-launcher:go_default_library",
        "//pkg/virt-launcher/metadata:go_default_library",
        "//pkg/virt-launcher/notify-client:go_default_library",
        "//pkg/virt-launcher/standalone:go_default_library",
        "//pkg/virt-launcher/virtwrap:go_default_library",
        "//pkg/virt-launcher/virtwrap/agent-poller:go_default_library",
        "//pkg/virt-launcher/virtwrap/api:go_default_library",
        "//pkg/virt-launcher/virtwrap/cli:go_default_library",
        "//pkg/virt-launcher/virtwrap/cmd-server:go_default_library",
        "//pkg/virt-launcher/virtwrap/util:go_default_library",
        "//staging/src/kubevirt.io/api/core/v1:go_default_library",
        "//staging/src/kubevirt.io/client-go/log:go_default_library",
        "//vendor/github.com/spf13/pflag:go_default_library",
        "//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
        "//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
        "//vendor/libvirt.org/go/libvirt:go_default_library",
    ],
)

go_binary(
    name = "virt-launcher",
    embed = [":go_default_library"],
    visibility = ["//visibility:public"],
    x_defs = version_x_defs(),
)

pkg_tar(
    name = "virt-launcher-tar",
    srcs = [
        "node-labeller/node-labeller.sh",
        ":virt-launcher",
        "//cmd/container-disk-v2alpha:container-disk",
        "//cmd/virt-freezer",
        "//cmd/virt-launcher-monitor",
        "//cmd/virt-probe",
        "//cmd/virt-tail",
    ],
    package_dir = "/usr/bin",
)

xattrs(
    name = "setcaps",
    capabilities = {
        "/usr/bin/virt-launcher-monitor": [
            "cap_net_bind_service",
        ],
    },
    tar = ":virt-launcher-tar",
)

passwd_entry(
    name = "root-user",
    gid = 0,
    home = "/root",
    info = "root",
    shell = "/bin/bash",
    uid = 0,
    username = "root",
)

group_entry(
    name = "qemu-group",
    gid = 107,
    groupname = "qemu",
)

group_entry(
    name = "root-group",
    gid = 0,
    groupname = "root",
)

group_file(
    name = "group",
    entries = [
        ":qemu-group",
        ":root-group",
    ],
)

passwd_entry(
    name = "qemu-user",
    gid = 107,
    home = "/home/qemu",
    shell = "/bin/bash",
    uid = 107,
    username = "qemu",
)

passwd_file(
    name = "passwd",
    entries = [
        ":qemu-user",
        ":root-user",
    ],
)

pkg_tar(
    name = "libvirt-config",
    srcs = [
        ":qemu.conf",
        ":virtqemud.conf",
    ],
    package_dir = "/etc/libvirt",
)

pkg_tar(
    name = "passwd-tar",
    srcs = [
        ":group",
        ":passwd",
    ],
    mode = "0644",
    package_dir = "etc",
    visibility = ["//visibility:public"],
)

pkg_tar(
    name = "nsswitch-tar",
    srcs = [
        ":nsswitch.conf",
    ],
    mode = "0644",
    package_dir = "/etc",
)

pkg_tar(
    name = "qemu-hook-tar",
    srcs = [
        ":qemu",
    ],
    mode = "0755",
    package_dir = "/etc/libvirt/hooks",
)

# Workaround for https://github.com/moby/moby/issues/44106
# Need to create the directory upfront, otherwise it gets assigned wrong
# permissions when unpacked.
pkg_tar(
    name = "qemu-kvm-modules-dir-tar",
    empty_dirs = [
        "usr/lib64/qemu-kvm",
    ],
    mode = "0755",
    owner = "0.0",
)

pkg_tar(
    name = "get-version-tar",
    srcs = ["//:get-version"],
    package_dir = "/",
)

oci_image(
    name = "version-container",
    base = select({
        "@io_bazel_rules_go//go/platform:linux_arm64": "@go_image_base_aarch64",
        "@io_bazel_rules_go//go/platform:linux_s390x": "@go_image_base_s390x",
        "//conditions:default": "@go_image_base",
    }),
    tars = select({
        "@io_bazel_rules_go//go/platform:linux_arm64": [
            "//rpm:launcherbase_aarch64",
            ":get-version-tar",
            ":libvirt-config",
            ":passwd-tar",
            ":nsswitch-tar",
            ":qemu-kvm-modules-dir-tar",
        ],
        "@io_bazel_rules_go//go/platform:linux_s390x": [
            "//rpm:launcherbase_s390x",
            ":get-version-tar",
            ":libvirt-config",
            ":passwd-tar",
            ":nsswitch-tar",
            ":qemu-kvm-modules-dir-tar",
        ],
        "//conditions:default": [
            "//rpm:launcherbase_x86_64",
            ":get-version-tar",
            ":libvirt-config",
            ":passwd-tar",
            ":nsswitch-tar",
            ":qemu-hook-tar",
            ":qemu-kvm-modules-dir-tar",
        ],
    }),
)

oci_image(
    name = "virt-launcher-image",
    base = ":version-container",
    entrypoint = ["/usr/bin/virt-launcher"],
    tars = [":setcaps"],
    visibility = ["//visibility:public"],
)
